/*
 * @Author: wuhao930406 1148547900@qq.com
 * @Date: 2023-09-13 13:54:09
 * @LastEditors: wuhao930406 1148547900@qq.com
 * @LastEditTime: 2023-10-27 15:18:28
 * @FilePath: /utools_tasks/utools_web/src/components/ItemCard/index.jsx
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */

import { ifdiffes } from '@/utils/time';
import { useModel } from '@umijs/max';
import { Progress, theme, Tooltip } from 'antd';
import dayjs from 'dayjs';
import { useMemo } from 'react';
import IconFont from '../IconFont';
import TagList from '../TagList';
import  './index.less';



function MostCard({ item, tipRender, style ,bg}) {
  const { token } = theme.useToken();

  const {
    colorText,
    colorBgSecondary,
    colorPrimary,
    colorBgThird,
    colorWarning,
    colorError,
  } = token;

  const {
    initialState: { currentUser },
    setInitialState,
  } = useModel('@@initialState');

  const ifStyle = useMemo(() => {
    const ifshowstype = ifdiffes(
      dayjs(dayjs(item?.planEnd ?? dayjs().add('day', -1)).format('YYYY-MM-DD') + ' 18:00:00'),
      dayjs(),
      currentUser,
      item,
    );

    if (ifshowstype) {
      if (currentUser.waringType === true) {
        if (ifshowstype === 'overtime') {
          return 'cardError';
        } else {
          return 'cardWaring';
        }
      } else {
        if (ifshowstype === 'overtime') {
          return 'cardError';
        } else {
          return 'cardWaring';
        }
      }
    }
    return null;
  }, [currentUser?.waringType, currentUser?.waringTime, item]);

  return (
    <div
      className={`bg itemcard`}
      style={{
        ...style,
        backgroundColor: bg ? bg : colorBgSecondary,
        color: colorText,
        position: 'relative',
        borderLeft: `4px solid ${
          ifStyle === 'cardWaring'
            ? colorWarning
            : ifStyle === 'cardError'
            ? colorError
            : 'transparent'
        }`,
        boxSizing: 'border-box',
      }}
      onClick={() => {
        setInitialState((s) => ({
          ...s,
          drawerAll: {
            ...s.drawerAll,
            item: item,
            open: true,
          },
        }));
      }}
    >
      <div className="spread" style={{ width: '100%' }}>
        <div className="center" style={{ flex: 1 }}>
          <Progress
            style={{ margin: '0 8px 0 0', opacity: 0.7 }}
            percent={item?.progress}
            strokeColor={colorPrimary}
            size={20}
            type="circle"
            trailColor={colorBgThird}
            format={(percent) => {
              return percent + '% ';
            }}
          />
          <b
            style={{
              fontSize: 15,
              flex: 1,
              wordBreak: 'break-all',
            }}
          >
            <span style={{ fontSize: 12, color: '#999', paddingRight: 6 }}>{item?.missionNo}</span>
          </b>

          {tipRender}
        </div>
        <div className="center" style={{ paddingLeft: 6, gap: 6 }}>
          {item?.type === 2 && (
            <IconFont type="icon-bugreport" style={{ fontSize: 16, color: '#ff4800' }}></IconFont>
          )}
        </div>
      </div>
      <div style={{ margin: '6px 0 0 0', lineHeight: 1.3 }}>
        <Tooltip title={item?.missionName}>
          <b className="oneline">{item?.missionName}</b>
        </Tooltip>
      </div>

      {!item?.actualTime && !item?.planTime ? null : (
        <div style={{ fontSize: 12, margin: '4px 0' }}>
          <span
            style={{
              color:
                !item?.planTime || item?.actualTime < item?.planTime ? colorText : colorPrimary,
            }}
          >
            消耗 {item?.actualTime ?? '0'} 小时
          </span>
          / 计划{item?.planTime ?? '-'}小时
        </div>
      )}

      {item?.itemTagList?.length > 0 && (
        <div className="center" style={{ justifyContent: 'flex-start', margin: '6px 0 0 0' }}>
          <TagList item={item} limit={2}></TagList>
        </div>
      )}
    </div>
  );
}

export default MostCard;
